package com.sinoglobal.ningxia.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.beans.ProgramaListBean;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by XINDZ on 2017/2/18.
 * 功能:视频模块具体栏目的adapter
 */

public class RecommendFramentAdapter extends BaseAdapter {
    private Context context;
    private List<ProgramaListBean.ListBean.LanmuBean> beanList = new ArrayList();
    private LayoutInflater layoutInflater;

    public RecommendFramentAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public RecommendFramentAdapter(Context context, List beanList) {
        this.context = context;
        this.beanList = beanList;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setData(List beanList) {
        this.beanList = beanList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.item_frag_recommend, null);
            viewHolder.jcRecommendVideoplayer = (JCVideoPlayerStandard) view.findViewById(R.id.jc_recommend_videoplayer);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_item_recommend_title);
            viewHolder.tvLookNum = (TextView) view.findViewById(R.id.tv_item_recommend_looknum);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        ProgramaListBean.ListBean.LanmuBean lanmuBean = (ProgramaListBean.ListBean.LanmuBean) beanList.get(i);
        String name = lanmuBean.getName();
        String video_img = lanmuBean.getVideo_img();
        String video_url = lanmuBean.getVideo_url();
        String r = lanmuBean.getPeriod();
        int sums = lanmuBean.getSums();
        String lanmuname = lanmuBean.getLanmuname();//栏目名
        if (r != null && !r.equals("")) {
            r = "    期号:" + r;
        }
        if (lanmuname == null) {
            lanmuname = "";
        }
        viewHolder.tvTitle.setText(lanmuname);
        viewHolder.tvLookNum.setText(sums + "次播放");

        if (video_img != null && !video_img.equals("")) {
            String url = ConnectionUtil.localUrl + video_img;
            Picasso.with(context).load(url).placeholder(context.getResources().getDrawable(R.drawable.default21))
                    .error(context.getResources().getDrawable(R.drawable.default21))
                    .into(viewHolder.jcRecommendVideoplayer.thumbImageView);
            viewHolder.jcRecommendVideoplayer.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        Logger.i("视频URL:" + video_url);
        viewHolder.jcRecommendVideoplayer.setUp(video_url, JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, name);
        viewHolder.jcRecommendVideoplayer.titleTextView.setMaxLines(2);
        viewHolder.jcRecommendVideoplayer.titleTextView.setEllipsize(TextUtils.TruncateAt.END);
//        viewHolder.jcRecommendVideoplayer.widthRatio = 16;
//        viewHolder.jcRecommendVideoplayer.heightRatio = 9;
        return view;
    }

    static class ViewHolder {
        JCVideoPlayerStandard jcRecommendVideoplayer;
        TextView tvTitle, tvLookNum;
    }

}
